<?php


namespace Xiaoguai\DesignPattern\DP006\Temp;


use Traversable;

class RedisCache implements \IteratorAggregate
{
    protected $list = [];

    public function add($object)
    {
        $this->list[] = $object;
    }

    public function remove($object)
    {
        $index = array_search($object, $this->list, true);
        if ($index !== false) {
            array_splice($this->list, $index, 1);
        }
    }
    
    /**
     * Retrieve an external iterator
     * @link https://php.net/manual/en/iteratoraggregate.getiterator.php
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
     * <b>Traversable</b>
     * @since 5.0.0
     */
    public function getIterator()
    {
        return new RedisCacheIterator($this->list);
    }
}